﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Controls;
using Merise_designer.Objets;
using Merise_designer.Methodes;
using Merise_designer.UItools;


namespace Merise_designer
{
    public partial class Edition : Form
    {
        private Objets.CL_Entite entite;
        private Objets.CL_Association association;
        private Methodes.ObjetsCommun controller;
        List<CL_DictionnaireDeDonnees> dataTemp = new List<CL_DictionnaireDeDonnees>();
        List<CL_DictionnaireDeDonnees> dataSource = new List<CL_DictionnaireDeDonnees>();

        //Contructeur
        public Edition(Methodes.ObjetsCommun contructor)
        {
            InitializeComponent();
            this.controller = contructor;
            this.dgInfos.RowHeadersVisible = false;
        }

        //Affichage
        public void AfficherEntite(Objets.CL_Entite entite, Methodes.ObjetsCommun contructor)
        {
            if (entite == null) return;

            foreach (CL_DictionnaireDeDonnees d in this.controller.getContructorDico().selectAllFromEntite(entite, "monjeu"))
            {
                dataSource.Add((CL_DictionnaireDeDonnees)d.Clone());
            }

            dataTemp.AddRange(dataSource);
            this.refreshInfos();

            this.association = null;
            this.entite = entite;
            this.txtName.Text = this.entite.Name;
            this.txtCode.Text = this.entite.CodeName;
            this.controller = contructor;
            this.StartPosition = FormStartPosition.CenterScreen;

            try { this.ShowDialog(); }
            catch { }
        }
        public void AfficherAsso(Objets.CL_Association asso, Methodes.ObjetsCommun contructor)
        {
            if (asso == null) return;

            foreach (CL_DictionnaireDeDonnees d in this.controller.getContructorDico().selectAllFromAssociation(asso, "monjeu"))
            {
                dataSource.Add((CL_DictionnaireDeDonnees)d.Clone());
            }

            dataTemp.AddRange(dataSource);
            this.refreshInfos();

            this.entite = null;
            this.association = asso;
            this.txtName.Text = this.association.Name;
            this.txtCode.Text = this.association.CodeName;
            this.controller = contructor;
            
            try { this.ShowDialog(); }
            catch { }
        }

        //Raffraichissement
        private void refreshInfos()
        {
            int iColumn = dgInfos.CurrentCell == null ? 2 : dgInfos.CurrentCell.ColumnIndex;
            int iRow = dgInfos.CurrentCell == null ? 0 : dgInfos.CurrentCell.RowIndex;

            dataSource = new List<CL_DictionnaireDeDonnees>();

            dataTemp.Sort(CL_DictionnaireDeDonnees.idComparaison);
            dataSource.AddRange(dataTemp);
            
            dataSource.Sort(CL_DictionnaireDeDonnees.idComparaison);
            this.dgInfos.DataSource = dataSource;
            
            this.dgInfos.Columns[0].Visible = false;
            this.dgInfos.Columns[1].Visible = false;
            this.dgInfos.Columns[4].Visible = false;
            this.dgInfos.Columns[6].Visible = false;
            this.dgInfos.Columns[7].Visible = false;
            this.dgInfos.Columns[10].Visible = false;
            this.dgInfos.Columns[11].Visible = false;

            this.dgInfos.Columns[5].ReadOnly = true;
            this.dgInfos.Columns[10].ReadOnly = true;

            //this.dgInfos.Sort(this.dgInfos.Columns[1], ListSortDirection.Ascending);

            if (iColumn >= 0 && iRow >= 0 && this.dgInfos.CurrentCell != null)
            {
                if (this.dgInfos.Rows.Count > 0) dgInfos.CurrentCell = dgInfos[iColumn, iRow];
            }
        }

        //Modification du nom de l'entité ou de l'asso
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            txtCode.Text = txtName.Text.ToString().Replace(" ", "_").ToUpper();
        }

        public delegate void InvokeDelegate();
        private void dgInfos_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            this.BeginInvoke(new InvokeDelegate(ModifyGrid));
        }
        //Evenement check box
        private void dgInfos_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            this.BeginInvoke(new InvokeDelegate(ModifyGrid));
        }

        private void ModifyGrid()
        {
            if (dataTemp.Count > 0)
            {
                if (this.dgInfos.CurrentRow.Index >= 0)
                {
                    Objets.CL_DictionnaireDeDonnees d = this.select((int)dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[1].Value);
                    String code = dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[3].Value != null ? dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[3].Value.ToString() : "";
                    if(d.nom!=null) d.code = (code == "INDEFINIT" || code == "") ? d.nom.Replace(" ", "_").ToUpper() : code;
                    if (d.pk) d.O = true; //Force le champs Obligatoire si clé primaire
                    d.stringO = this.controller.getContructorDico().getStringO(d.O);
                    this.refreshInfos();
                }
            }
        }

        //Modification du type
        private void dgInfos_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (this.dgInfos.CurrentRow.Index >= 0 && this.dgInfos.CurrentCell.ColumnIndex == 5)
                {

                    Types fTypes = new Types();
                    int choixType = fTypes.Afficher(Convert.ToInt32(dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[4].Value.ToString()), this.controller);
                    if (choixType > -1)
                    {
                        Objets.CL_DictionnaireDeDonnees d = this.select((int)dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[1].Value);
                        if (d != null)
                        {
                            d.idType = choixType;
                            d.stringType = this.controller.getContructorType().get(d.idType).nom;
                            //d.stringO = this.controller.getContructorDico().getStringO((Boolean)dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[10].Value);
                            this.refreshInfos();
                        }
                    }
                }
            }
            catch { }
        }
        private void Edition_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.controller.updateMode();
        }
        private void addNew_Click(object sender, EventArgs e)
        {
            if (this.association == null)
            {
                dataTemp.Add(controller.getContructorDico().getNewSimpleEntite(controller, entite));
            }
            else if (this.entite == null)
            {
                dataTemp.Add(controller.getContructorDico().getNewSimpleAssociation(controller, association));
            }

            this.refreshInfos();

            this.dgInfos.CurrentCell = this.dgInfos[2, dgInfos.Rows.Count - 1];
            this.dgInfos.BeginEdit(true);
        }
        private void btDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dgInfos.CurrentRow.Index >= 0)
                {
                    dataTemp.Remove(this.select((int)dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[1].Value));
                    this.refreshInfos();
                }
            }
            catch { }
        }

        //Boutons de la fenêtre
        private void btValid_Click(object sender, EventArgs e)
        {

            if (this.association == null)
            {
                //Entié
                //données
                this.controller.getContructorDico().deleteAllFromEntite(entite);
                foreach (Objets.CL_DictionnaireDeDonnees d in dataTemp)
                {
                    this.controller.getContructorDico().add(this.controller, d);
                }
                //titre
                this.entite.Name = this.txtName.Text;
                this.entite.CodeName = this.txtCode.Text;
                this.controller.updateEntite(this.entite);
                this.controller.CreateBackUpFIle(); //Pour les opérations Annuler/Rétablir

            }
            else if (this.entite == null)
            {
                //Association
                //données
                this.controller.getContructorDico().deleteAllFromAssociation(association);
                foreach (Objets.CL_DictionnaireDeDonnees d in dataTemp)
                {
                    this.controller.getContructorDico().add(this.controller, d);
                }
                //titre
                this.association.Name = this.txtName.Text;
                this.association.CodeName = this.txtCode.Text;
                this.controller.updateAsso(this.association);
                this.controller.CreateBackUpFIle(); //Pour les opérations Annuler/Rétablir
            }
            this.Close();
        }
        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Raccourcis clavier
        private void dgInfos_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Delete)
            {
                btDelete_Click(null, null);
            }
            if (e.KeyCode == System.Windows.Forms.Keys.Add)
            {
                addNew_Click(null, null);
            }

            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {
                this.dgInfos_MouseClick(null, null);
            }
        }

        //Autres méthodes
        private Boolean getBoolResult(String value)
        {
            if (value == null | value == "")
            {
                return false;
            }
            else
            {
                return Boolean.Parse(value);
            }
        }
        private Objets.CL_DictionnaireDeDonnees select(int id)
        {
            foreach (Objets.CL_DictionnaireDeDonnees d in dataTemp)
            {
                if (d.id == id) return d;
            }
            return null;
        }

        private void btUp_Click(object sender, EventArgs e)
        {
            if (this.dgInfos.CurrentRow.Index > 0)
            {
                int iColumn = dgInfos.CurrentCell == null ? 2 : dgInfos.CurrentCell.ColumnIndex;
                int iRow = dgInfos.CurrentCell == null ? 0 : dgInfos.CurrentCell.RowIndex;

                Objets.CL_DictionnaireDeDonnees d1 = this.select((int)dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[1].Value);
                String code = dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[3].Value != null ? dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[3].Value.ToString() : "";
                if (d1.nom != null) d1.code = (code == "INDEFINIT" || code == "") ? d1.nom.Replace(" ", "_").ToUpper() : code;
                if (d1.pk) d1.O = true; //Force le champs Obligatoire si clé primaire
                d1.stringO = this.controller.getContructorDico().getStringO(d1.O);

                Objets.CL_DictionnaireDeDonnees d2 = this.select((int)dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[1].Value-1);

                //inversion des ID
                int id = d1.id;
                d1.id = d2.id;
                d2.id = id;

                //Reconstruction des listes
                List<CL_DictionnaireDeDonnees> _mem = new List<CL_DictionnaireDeDonnees>();
                _mem.AddRange(dataTemp);
                dataTemp = new List<CL_DictionnaireDeDonnees>();
                dataTemp.AddRange(_mem);

                if (iColumn >= 0 && iRow >= 0 && this.dgInfos.CurrentCell != null)
                {
                    if (this.dgInfos.Rows.Count > 0) dgInfos.CurrentCell = dgInfos[iColumn, iRow-1];
                }

                this.refreshInfos();
            }
        }

        private void btDown_Click(object sender, EventArgs e)
        {
            if (this.dgInfos.CurrentRow.Index <dataTemp.Count-1)
            {
                int iColumn = dgInfos.CurrentCell == null ? 2 : dgInfos.CurrentCell.ColumnIndex;
                int iRow = dgInfos.CurrentCell == null ? 0 : dgInfos.CurrentCell.RowIndex;

                Objets.CL_DictionnaireDeDonnees d1 = this.select((int)dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[1].Value);
                String code = dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[3].Value != null ? dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[3].Value.ToString() : "";
                if (d1.nom != null) d1.code = (code == "INDEFINIT" || code == "") ? d1.nom.Replace(" ", "_").ToUpper() : code;
                if (d1.pk) d1.O = true; //Force le champs Obligatoire si clé primaire
                d1.stringO = this.controller.getContructorDico().getStringO(d1.O);

                Objets.CL_DictionnaireDeDonnees d2 = this.select((int)dgInfos.Rows[dgInfos.CurrentRow.Index].Cells[1].Value + 1);

                //inversion des ID
                int id = d1.id;
                d1.id = d2.id;
                d2.id = id;

                //Reconstruction des listes
                List<CL_DictionnaireDeDonnees> _mem = new List<CL_DictionnaireDeDonnees>();
                _mem.AddRange(dataTemp);
                dataTemp = new List<CL_DictionnaireDeDonnees>();
                dataTemp.AddRange(_mem);

                if (iColumn >= 0 && iRow >= 0 && this.dgInfos.CurrentCell != null)
                {
                    if (this.dgInfos.Rows.Count > 0) dgInfos.CurrentCell = dgInfos[iColumn, iRow + 1];
                }

                this.refreshInfos();
            }
        }



    }

}
